import 'package:flutter_test/flutter_test.dart';
import 'package:awak/common/models/content.dart';
import 'package:awak/common/models/topic.dart';
import 'package:awak/common/models/user.dart';
import 'package:awak/common/repository/content_repository.dart';

void main() {
  group('Content Topic Integration Tests', () {
    test('Content model should include topicId field', () {
      final content = Content(
        id: 1,
        picture: 'test.jpg',
        description: 'Test description',
        userId: 1,
        topicId: 1,
      );

      expect(content.topicId, 1);
      expect(content.picture, 'test.jpg');
      expect(content.description, 'Test description');
      expect(content.userId, 1);
    });

    test('Content toMap should include topicId', () {
      final content = Content(
        id: 1,
        picture: 'test.jpg',
        description: 'Test description',
        userId: 1,
        topicId: 2,
      );

      final map = content.toMap();
      expect(map['topicId'], 2);
      expect(map['picture'], 'test.jpg');
      expect(map['description'], 'Test description');
      expect(map['userId'], 1);
    });

    test('Content fromMap should include topicId', () {
      final map = {
        'id': 1,
        'picture': 'test.jpg',
        'description': 'Test description',
        'userId': 1,
        'topicId': 3,
      };

      final content = Content.fromMap(map);
      expect(content.topicId, 3);
      expect(content.picture, 'test.jpg');
      expect(content.description, 'Test description');
      expect(content.userId, 1);
    });

    test('Content copyWith should handle topicId', () {
      final original = Content(
        id: 1,
        picture: 'original.jpg',
        description: 'Original description',
        userId: 1,
        topicId: 1,
      );

      final updated = original.copyWith(
        topicId: 2,
        description: 'Updated description',
      );

      expect(updated.topicId, 2);
      expect(updated.description, 'Updated description');
      expect(updated.picture, 'original.jpg'); // 未更改的字段保持原值
      expect(updated.userId, 1);
    });

    test('ContentRepository should have methods for topic-based queries', () {
      final repository = ContentRepository();
      
      // 验证方法存在（编译时检查）
      expect(repository.getContentsByTopicId, isA<Function>());
      expect(repository.getContentCountByTopicId, isA<Function>());
      expect(repository.getContentsByUserId, isA<Function>());
      expect(repository.getAllContents, isA<Function>());
    });

    test('Topic and Content relationship should be logical', () {
      // 创建主题
      final topic = Topic(
        id: 1,
        title: 'Philosophy Topic',
        coverImage: 'topic.jpg',
        content: '5', // 这个字段现在表示内容数量
        label: 'philosophy',
        numberPeople: 98,
        userId: 1,
      );

      // 创建属于该主题的内容
      final contents = [
        Content(
          id: 1,
          picture: 'content1.jpg',
          description: 'First philosophical content',
          userId: 1,
          topicId: topic.id!,
        ),
        Content(
          id: 2,
          picture: 'content2.jpg',
          description: 'Second philosophical content',
          userId: 2,
          topicId: topic.id!,
        ),
      ];

      // 验证关系
      for (final content in contents) {
        expect(content.topicId, topic.id);
      }

      // 验证内容数量与主题的content字段一致性
      final expectedCount = int.tryParse(topic.content) ?? 0;
      expect(expectedCount, greaterThanOrEqualTo(0));
    });

    test('User and Content relationship should work', () {
      final user = User(
        id: 1,
        username: 'testuser',
        password: 'password',
        nickname: 'Test User',
        avatar: 'avatar.jpg',
        signature: 'Test signature',
      );

      final content = Content(
        id: 1,
        picture: 'content.jpg',
        description: 'User generated content',
        userId: user.id!,
        topicId: 1,
      );

      expect(content.userId, user.id);
    });

    test('Content model should handle all required fields', () {
      // 测试所有必需字段都存在
      expect(() => Content(
        picture: 'test.jpg',
        description: 'Test description',
        userId: 1,
        topicId: 1,
      ), returnsNormally);

      // 测试缺少topicId会导致编译错误（这个测试确保topicId是必需的）
      // 注意：这个测试主要是为了确保API设计正确
    });
  });
}
